package command;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import app.OrderApp;
import command_impl.*;
import pojo.User;

public class UserCommandFactory extends SystemCommandFactory {

	private User user = OrderApp.getInstance().getUser();
	private static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();
	static{
		commandMap.put(CommandCode.BUY, BuyCommand.class);
		commandMap.put(CommandCode.DEL, DelCommand.class);
		commandMap.put(CommandCode.CHARGE, ChargeCommand.class);
		commandMap.put(CommandCode.WITHDRAW, WithdrawCommand.class);
		commandMap.put(CommandCode.BALANCE, BalanceCommand.class);
		commandMap.put(CommandCode.JOIN, JoinCommand.class);
		commandMap.put(CommandCode.ALLORDERS, AllOrdersCommand.class);
		commandMap.put(CommandCode.LOGOUT, LogoutCommand.class);
		commandMap.put(CommandCode.PAY, PayCommand.class);
	}
	
	public UserCommandFactory(User user) {
	}

	
	@Override
	public Command buildCommand(CommandCode cmd) {
		Class<? extends Command> cmdClass = commandMap.get(cmd);
		
		if(!commandMap.containsKey(cmd)){
			return new SystemCommandFactory().buildCommand(cmd);
		}
		
		if(cmdClass!=null){
			try {
				return cmdClass.getConstructor(User.class).newInstance(user);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
